package class_0826HashTable;

/**
 * 如果看得迷糊的话，
 */
public class Coding_BitMap {
    static{
        int[] arr = new int[10];
        int i = 178;
        int numIndex = 178/32;
        int bitIndex = 178%32;
    }
    public static class BitMap {
        /**
         * BitSets are packed into arrays of "words."
         *
         */
        private int[] word;
        private static final int TYPE_SIZE_32INT = 32;

        /**
         *
         * @param initCapacity
         */
        public BitMap(int initCapacity) {
            word = new int[(initCapacity + TYPE_SIZE_32INT - 1) / TYPE_SIZE_32INT];
        }

        public BitMap(){
            this(1);//8 bit
        }
        public int get(int val) throws IndexOutOfBoundsException{
            if(val<0){
                throw new IndexOutOfBoundsException();
            }
            int numIndex = val/TYPE_SIZE_32INT;
            int bitIndex = val%TYPE_SIZE_32INT;

            return word[numIndex]>>bitIndex & 1;
        }
        public boolean contains(int val) throws IndexOutOfBoundsException{
            return get(val)!=0;
        }
        public boolean setOne(int val){
            if(val<0){
                throw new IndexOutOfBoundsException();
            }
            int numIndex = val/TYPE_SIZE_32INT;
            int bitIndex = val%TYPE_SIZE_32INT;
            word[numIndex] = word[numIndex] | (1<<bitIndex);
            return true;
        }
        public boolean setZero(int val){
            if(val < 0 || val >= word.length * TYPE_SIZE_32INT) {
                throw new IndexOutOfBoundsException();
            }

            int numIndex = val/TYPE_SIZE_32INT;
            int bitIndex = val%TYPE_SIZE_32INT;
            word[numIndex] = word[numIndex] & (~(1<<bitIndex));
            return true;
        }
    }
}
